﻿package fr.minuit4.components 
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import mx.containers.Panel;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import flash.events.MouseEvent;
	import fr.minuit4.events.drawEvent;
	import flash.events.Event;
	
	public class drawingZone extends Panel
	{
		private var _UIConteneur:UIComponent;
		private var _XBuffer:int;
		private var _YBuffer:int;
		private var _Zone:Sprite;
		
		private var _BgColorDrawingZone:uint = 0xFFFFFFF;
		private var _lineColor:uint = 0x000000;
		private var _lineThickness:int = 1;
		
		
		/**
		 * Constructeur
		 */
		public function drawingZone() 
		{
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE, Init);
		}
		
		
		
		/**
		 * Méthode public de ré-initialisation du tracé
		 */
		public function EraseDraw():void
		{
			this.removeChild(_UIConteneur);
			_UIConteneur = null;
			InitConteneur();
			InitBackground();
		}
		
		/**
		 * Méthode d'initialisation de la drawingZone appelée dès que l'évènement creationComplete est déclenché
		 * @param	evt		FlexEvent		Evènement écouté
		 */
		private function Init(e:FlexEvent):void
		{
			InitConteneur();
			InitBackground();
		}
		
		
		/**
		 * Initialisation et ajout à la liste d'affichage du conteneur
		 */
		private function InitConteneur():void
		{
			_UIConteneur = new UIComponent();
			this.addChild(_UIConteneur);
		}
		
		
		/**
		 * initialisation du sprite conteneur du dessin, ajout à la liste d'affichage et ajout des EventListener de la souris sur ce sprite
		 */
		private function InitBackground():void
		{
			_Zone = new Sprite();
			_Zone.graphics.beginFill(BgColorDrawingZone, 1);
			_Zone.graphics.drawRect(0, 0, (this.width-this.borderMetrics.left-this.borderMetrics.right), (this.height-this.borderMetrics.top-this.borderMetrics.bottom));
			_Zone.graphics.endFill();

			_UIConteneur.addChild(_Zone);
			_Zone.addEventListener(MouseEvent.MOUSE_DOWN, start_Draw);
			_Zone.addEventListener(MouseEvent.MOUSE_UP, stop_Draw);
		}
		
		
		
		/**
		 * Méthode écoutant l'évènement MOUSE_UP stoppant le tracé en cours
		 * @param	e		MouseEvent		Evènement écouté
		 */
		private function stop_Draw(e:MouseEvent):void 
		{
			var zone:Sprite = e.target as Sprite;
			zone.removeEventListener(Event.ENTER_FRAME, Drawing);
		}
		
		
		/**
		 * Méthode écoutant l'évènement MOUSE_DOWN marquant le début du tracé
		 * @param	e		MouseEvent		Evènement écouté
		 */
		private function start_Draw(e:MouseEvent):void 
		{
			var zone:Sprite = e.target as Sprite;
			dispatchEvent(new drawEvent(drawEvent.START_DRAW));
			_XBuffer = mouseX - this.borderMetrics.left;
			_YBuffer = mouseY - this.borderMetrics.top;
			zone.graphics.lineStyle(lineThickness, lineColor, 1);
			zone.graphics.moveTo(_XBuffer,_YBuffer);
			zone.addEventListener(Event.ENTER_FRAME, Drawing);
		}
		
		
		/**
		 * Méthode écoutant l'évènement ENTER_FRAME déclenchant la poursuite du tracé
		 * @param	e		Event		Evènement écouté
		 */
		private function Drawing(e:Event):void 
		{
			var zone:Sprite = e.target as Sprite;
			var currentX:int = mouseX - this.borderMetrics.left;
			var currentY:int = mouseY - this.borderMetrics.top;
			if (currentX < 0  || currentY < 0 || currentX > (this.width - this.borderMetrics.left - this.borderMetrics.right) || currentY > (this.height - this.borderMetrics.top - this.borderMetrics.bottom))
				zone.removeEventListener(Event.ENTER_FRAME, Drawing);
			zone.graphics.curveTo(_XBuffer, _YBuffer, (_XBuffer + currentX)/2, (_YBuffer+currentY)/2);
			_XBuffer = currentX;
			_YBuffer = currentY;
		}
		
		
		/*--GETTER--SETTER--*/
		
		public function get BgColorDrawingZone():uint { return _BgColorDrawingZone; }
		
		public function set BgColorDrawingZone(value:uint):void 
		{
			_BgColorDrawingZone = value;
		}
		
		public function get lineColor():uint { return _lineColor; }
		
		public function set lineColor(value:uint):void 
		{
			_lineColor = value;
		}
		
		public function get lineThickness():int { return _lineThickness; }
		
		public function set lineThickness(value:int):void 
		{
			_lineThickness = value;
		}
		
		public function get UIConteneur():UIComponent { return _UIConteneur; }
		
	}
	
}